package com.yimingkeji.redis.rest;

import com.yimingkeji.redis.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

/**
 * @Describe:
 * @Auther: zhenlong.yang
 * @Date: 2018/12/3 18:19
 */
@RestController
@RequestMapping("/redis")
public class RedisResource {
    @Autowired private StringRedisTemplate stringRedisTemplate;

    @Autowired private UserService userService;

    @PostMapping("/test")
    public String test(@RequestParam(defaultValue = "key") String key, @RequestParam(defaultValue = "val")String val){
        stringRedisTemplate.opsForValue().set(key, val);
        return "key:" + key + ", val=" + stringRedisTemplate.opsForValue().get(key);
    }

    /**--------------cacheManager测试--------------*/
    @GetMapping("/user")
    public UserService.User get(int id){
        return userService.get(id);
    }

    @PostMapping("/user")
    public UserService.User save(int id, String name){
        return userService.save(id, name);
    }

    @DeleteMapping("/user")
    public String del(int id){
        userService.del(id);
        return "delete success.";
    }
}
